package com.huihui.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.huihui.R;
import com.huihui.adapter.MineMsgSayAdapter;
import com.huihui.adapter.MineMsgZanAdapter;
import com.huihui.base.BaseActivity;
import com.huihui.bean.MinePingLun;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.widget.SwipeRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * 我的消息-评论
 */
public class MineMsgSayActivity extends BaseActivity {

    private SwipeRecyclerView recyclerView;
    private MineMsgSayAdapter adapter;
    private List<MinePingLun> mList = new ArrayList<>();

    private int pageIndex = 1;
    private int pageNum = 10;
    private LinearLayout inputLayout;
    private EditText inputText;
    private MinePingLun parentPinLun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_msg_say);
//        getSupportActionBar().setTitle(R.string.pinglun);
        titleView.setText(R.string.pinglun);
        initView();
        initData();
        initListener();
    }


    private void initListener() {

        inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {//搜索按键action
                    String content = inputText.getText().toString();
                    if (TextUtils.isEmpty(content)) {
                        hideInput(MineMsgSayActivity.this);
                        return false;
                    }
                    initComment(content);
                    inputLayout.setVisibility(View.GONE);
                    inputText.getText().delete(0,content.length());
                    hideInput(getContext());
                }
                return false;
            }
        });

        recyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                initData();
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                initData();
            }
        });
        adapter.setOnReplyClickListener(new MineMsgSayAdapter.OnReplyListener() {


            @Override
            public void onClick(int position) {
                parentPinLun = mList.get(position);
                showInputLayout("回复 " + parentPinLun.getSend_user_nickname());
            }
        });
    }

    private void initComment(String commemt) {
        if (parentPinLun == null) {
            return;
        }
        int class_id = parentPinLun.getClass_id();
        if (class_id == 0) {
            //商品
            addProductComment(HttpContants.product_addComment, commemt);
        } else if (class_id == 1) {
            //视频
            addVideoComment(HttpContants.video_addComment, commemt);
        } else if (class_id == 2) {
            //文章
            addArticleComment(HttpContants.article_addComment, commemt);

        }
    }

    private void addProductComment(String url, String content) {
        OkHttpRequest.getInstance().addProductComment(url, AppManager.getUserInfo().getUser_id(), parentPinLun.getRelation_id(),
                parentPinLun.getCurrent_comment_id(), content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void addArticleComment(String url, String content) {
        OkHttpRequest.getInstance().addArticleComment(url, AppManager.getUserInfo().getUser_id(), parentPinLun.getRelation_id(),
                parentPinLun.getCurrent_comment_id(), content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void addVideoComment(String url, String content) {
        OkHttpRequest.getInstance().addVideoComment(url, AppManager.getUserInfo().getUser_id(), parentPinLun.getRelation_id(),
                parentPinLun.getCurrent_comment_id(), content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void showInputLayout(String name) {
        inputLayout.setVisibility(View.VISIBLE);
        inputText.setHint(name);
        showInput2(getContext(), inputLayout);
    }

    private void initData() {
        if (getUserInfo() == null) {
            return;
        }
        OkHttpRequest.getInstance().msgList(HttpContants.msg_list, AppManager.getUserInfo().getUser_id(), 1, 10, pageIndex, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                recyclerView.setRefreshing(false);
            }

            @Override
            public void onResponse(String response) {
                recyclerView.setRefreshing(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject != null && jsonObject.has(HttpContants.DATA)) {
                        List<MinePingLun> list = AppManager.getGson().fromJson(jsonObject.getString(HttpContants.DATA), new TypeToken<List<MinePingLun>>() {
                        }.getType());
                        if (list != null) {
                            if (pageIndex == 1) {
                                mList.clear();
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            } else {
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                        }else{
                            recyclerView.setLoadMoreEnable(false);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void initView() {
        recyclerView = (SwipeRecyclerView) findViewById(R.id.recycler);
        inputLayout = findViewById(R.id.inuput_layout);
        inputText = findViewById(R.id.input_text);
        recyclerView.getSwipeRefreshLayout()
                .setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        setRecycleView(recyclerView);
        recyclerView.setRefreshing(true);

        adapter = new MineMsgSayAdapter(this, mList);
        recyclerView.setAdapter(adapter);
    }

}
